﻿using System;

namespace Ecobee.Net
{
	/// <summary>
	/// The runtime object represents the last known thermostat running state. This state is composed from 
	/// the last interval status message received from a thermostat. It is also updated each time the 
	/// thermostat posts configuration changes to the server. The runtime object is read-only.
	/// </summary>
	public class Runtime : ApiObject
	{
		private Revision _revision = null;		
		private bool _connected = false;
		private DateTime? _firstConnected = null;
		private DateTime? _connectDateTime = null;
		private DateTime? _disconnectDateTime = null;
		private DateTime? _lastModified = null;
		private DateTime? _lastStatusModified = null;
		private double? _actualTemperature = null;
		private double? _actualHumidity = null;
		private double? _desiredHeat = null;
		private double? _desiredCool = null;
		private double? _desiredHumidity = null;
		private double? _desiredDehumidity = null;

		/// <summary>
		///  The current runtime revision. Equivalent in meaning to the runtime revision 
		///  number in the thermostat summary call.
		/// </summary>
		public Revision Revision
		{
			get
			{
				return _revision;
			}
			set
			{
				this.SetProperty(ref _revision, value);
			}
		}

		/// <summary>
		/// Whether the thermostat is currently connected to the server.
		/// </summary>
		public bool Connected
		{
			get
			{
				return _connected;
			}
			set
			{
				this.SetProperty(ref _connected, value);
			}
		}

		/// <summary>
		/// The UTC date/time stamp of when the thermostat first connected to the ecobee server.
		/// </summary>
		public DateTime? FirstConnected
		{
			get
			{
				return _firstConnected;
			}
			set
			{
				this.SetProperty(ref _firstConnected, value);
			}
		}

		/// <summary>
		/// The last recorded connection date and time. 
		/// </summary>
		public DateTime? ConnectDateTime
		{
			get
			{
				return _connectDateTime;
			}
			set
			{
				this.SetProperty(ref _connectDateTime, value);
			}
		}

		/// <summary>
		/// The last recorded disconnection date and time.
		/// </summary>
		public DateTime? DisconnectDateTime
		{
			get
			{
				return _disconnectDateTime;
			}
			set
			{
				this.SetProperty(ref _disconnectDateTime, value);
			}
		}

		/// <summary>
		/// The UTC date/time stamp of when the thermostat was updated. Format: YYYY-MM-DD HH:MM:SS.
		/// </summary>
		public DateTime? LastModified
		{
			get
			{
				return _lastModified;
			}
			set
			{
				this.SetProperty(ref _lastModified, value);
			}
		}

		/// <summary>
		/// The UTC date/time stamp of when the thermostat last posted its runtime information. Format: YYYY-MM-DD HH:MM:SS.
		/// </summary>
		public DateTime? LastStatusModified
		{
			get
			{
				return _lastStatusModified;
			}
			set
			{
				this.SetProperty(ref _lastStatusModified, value);
			}
		}

		/// <summary>
		/// The current temperature displayed on the thermostat.
		/// </summary>
		public double? ActualTemperature
		{
			get
			{
				return _actualTemperature;
			}
			set
			{
				this.SetProperty(ref _actualTemperature, value);
			}
		}

		/// <summary>
		/// The current humidity % shown on the thermostat.
		/// </summary>
		public double? ActualHumidity
		{
			get
			{
				return _actualHumidity;
			}
			set
			{
				this.SetProperty(ref _actualHumidity, value);
			}
		}

		/// <summary>
		/// The desired heat temperature as per the current running program or active event.
		/// </summary>
		public double? DesiredHeat
		{
			get
			{
				return _desiredHeat;
			}
			set
			{
				this.SetProperty(ref _desiredHeat, value);
			}
		}

		/// <summary>
		/// The desired cool temperature as per the current running program or active event.
		/// </summary>
		public double? DesiredCool
		{
			get
			{
				return _desiredCool;
			}
			set
			{
				this.SetProperty(ref _desiredCool, value);
			}
		}

		/// <summary>
		/// The desired humidity set point.
		/// </summary>
		public double? DesiredHumidity
		{
			get
			{
				return _desiredHumidity;
			}
			set
			{
				this.SetProperty(ref _desiredHumidity, value);
			}
		}

		/// <summary>
		/// The desired dehumidification set point.
		/// </summary>
		public double? DesiredDehumidity
		{
			get
			{
				return _desiredDehumidity;
			}
			set
			{
				this.SetProperty(ref _desiredDehumidity, value);
			}
		}

		protected override void OnFromDataObject(dynamic dataObject)
		{
			this.Revision = new Revision() { Identifier = dataObject.Revision };
			this.Connected = dataObject.connected;
			this.FirstConnected = dataObject.firstConnected;
			this.ConnectDateTime = dataObject.connectDateTimed;
			this.DisconnectDateTime = dataObject.disconnectDateTime;
			this.LastModified = dataObject.lastModified;
			this.LastStatusModified = dataObject.lastStatusModified;
			this.ActualTemperature = dataObject.actualTemperature / 10.0;
			this.ActualHumidity = dataObject.actualHumidity / 10.0;
			this.DesiredHeat = dataObject.desiredHeat / 10.0;
			this.DesiredCool = dataObject.desiredCool / 10.0;
			this.DesiredDehumidity = dataObject.desiredDehumidity / 10.0;
			this.DesiredDehumidity = dataObject.DesiredDehumidity / 10.0;
		}

		protected override string OnUniqueKey()
		{
			return this.Revision;
		}

		protected override string OnGetKeyValue(dynamic dataObject)
		{
			return dataObject.revision;
		}
	}
}
